<template>
  <div class="bg-white my-[10px]">
    <BaseHead style="border-bottom: 0"
      >项目文档<el-button @click="handleAdd">添加</el-button></BaseHead
    >
    <el-table :data="documentFileList" class="w-full" border stripe>
      <el-table-column type="index" label="序号" width="60px" />
      <el-table-column label="文件名称">
        <template #default="{ row }">
          <FileUpload :uploadUrl="uploadUrl" v-model="row.fileList" tip="" :limit="1" />
        </template>
      </el-table-column>
      <el-table-column label="上传人">
        <template #default="{ row }">
          {{ row.fileList[0]?.uploadPersonName }}
        </template>
      </el-table-column>
      <el-table-column label="上传时间">
        <template #default="{ row }">
          {{ row.fileList[0]?.uploadTime }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100" align="center">
        <template #default="{ $index }">
          <el-button type="primary" size="small" plain @click="handleDel($index)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import FileUpload from "@/views/project/components/FileUpload.vue";
import { editDocument, documentDetail } from "@/api/project/zx/lxxm";
import { formatDate } from "@/utils/formatTime";
import useUserStore from "@/store/modules/user";
const uploadUrl = `/commonFile/uploadFile?modelName=纵向项目项目文档`;
const operateId = ref(null);
const userStore = useUserStore();
const documentFileList = ref([]);
const fileItem = {
  fileList: [],
};
const isAuditor = inject("isAuditor");
const taskId = inject("taskId");
const checkStatus = inject("checkStatus");
//点击添加按钮
const handleAdd = () => {
  documentFileList.value.push(Object.assign({}, fileItem));
};
//点击删除按钮删除一行
const handleDel = (index) => {
  documentFileList.value.splice(index, 1);
};
//获取文档信息详情
const getDocumentDetail = async () => {
  try {
    const res = await documentDetail(operateId.value);
    const fileList = res.data.projectDocumentList;
    fileList.forEach((item) => {
      documentFileList.value.push({
        fileList: [
          {
            name: item.filename,
            id: item.fileId,
            uploadPersonId: item.uploadPersonId,
            uploadPersonName: item.uploadPersonName,
            uploadTime: item.uploadTime,
          },
        ],
      });
    });
  } catch (error) {
    console.log(error);
  }
};
// 编辑
const edit = async (id) => {
  operateId.value = id;
  getDocumentDetail();
};
const emit = defineEmits(["save-success", "save-ok"]);
//保存
const save = async (submitType) => {
  try {
    const fileList = [];
    documentFileList.value.forEach((item) => {
      if (item.fileList.length) {
        const current = item.fileList[0];
        fileList.push({
          projectId: operateId.value,
          filename: current.name, //校级、纵向是filename 横向是fileName
          fileId: current.response ? current.response.data : current.id,
          uploadPersonId: current.uploadPersonId || userStore.userRealId,
          uploadPersonName: current.uploadPersonName || userStore.nickName,
          uploadTime: current.uploadTime || formatDate(new Date()),
        });
      }
    });
    let params = {
      proId: operateId.value,
      submitType,
      isAuditor: isAuditor.value,
      taskId: taskId.value,
      checkstatus: checkStatus.value,
      slaveTableList: fileList,
    };
    const res = await editDocument(params);
    if (res.code === 200) {
      emit("save-success");
    }
  } catch (error) {
    console.log(error);
  } finally {
    emit("save-ok");
  }
};
defineExpose({ edit, save });
</script>

<style scoped lang="scss"></style>
